package com.example.springaop3.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-03-13
 * Time: 23:25
 */


/**
 * 定义一个拦截器
 * 必须实现 HandlerInterceptor 接口
 * 并重写 preHandle 方法
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession(false);
        if(session!=null&&session.getAttribute("userinfo")!=null){
//            已登录
            return  true;
        }
//        未登录就跳转到 登录页面
        response.sendRedirect("/login.html");
        return false;
    }
}
